package controller.ProductController;

import bean.Product;
import com.fasterxml.jackson.databind.ObjectMapper;
import common.ResultAjax;
import services.ProductService;
import services.UserService;
import services.impl.ProductServiceImpl;
import services.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @auther hewenhui
 * @see <a href="hetp://www.codingmaster.com> java </a>
 */
@WebServlet("/home_index")
public class HomeServlet extends HttpServlet{
    ObjectMapper objectMapper = new ObjectMapper();
    //控制层调用服务层
    ProductService productService = new ProductServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/json; charset=utf-8");
        HttpSession session = req.getSession(false);
        if(session==null){
            String respJson = objectMapper.writeValueAsString(ResultAjax.fail(-1,"未登录，请先登录！"));
            resp.getWriter().write(respJson);
            return ;
        }
        String index = req.getParameter("index");


        List<Product> productList = productService.getProductByindex(index.trim());

        String respJson = objectMapper.writeValueAsString(ResultAjax.succeed(productList));
        resp.getWriter().write(respJson);


    }
}
